<?php

namespace application\api\controller;

use app\api\model\App as AppModel;
use think\Db;
use think\Request;

class App extends Cross
{
    public function index(Request $request)
    {
        $page = $request ->param('page');
        $limit = $request ->param('limit');

        $where = function ($query) use($request) {
            //搜索
            $key = $request ->param('key');
            if (!empty($key)) {
                $query-> where('title|name','like','%'.$key.'%');
            }
            //点击标签
            $tag = $request ->param('tag');
            if (!empty($tag)) {
                //trace($tag,"debug");
                $query-> where('tag','eq',$tag);
            }
        };
        $apps = AppModel::with(['app_versions'])->where($where)->paginate([
            "page" => $page,
            "list_rows" => $limit
        ]);
        return json($apps);
        //$db = new AppModel();
//        有关键字进行模糊查询
//        if (isset($key) && !empty($key)) {
//            $db = $db -> where('title|name','like','%'.$key.'%');
//        }
//        无关键字则直接查询
//        $info =  Db::view('m_app','id,name,title,intro,author,tag')
//            ->view('m_app_version','version','m_app_version.app_id=m_app.id','LEFT')
//            ->page($page)
//            ->order('m_app.id')
//            ->limit($limit)
//            ->select();
//        $info =Db::query("select m_app.title, GROUP_CONCAT(m_app_version.version) as version ,m_app.intro,m_app.author,m_app.tag  from m_app join m_app_version on (m_app.id = m_app_version.app_id) GROUP BY m_app.id");
//        $total = $db->count('id');
//        return json(['code'=>0,'data'=>$info,'total'=>$total]);

        /*$info = $db->page($page)->limit($limit)->join('m_app_version on a.id=b.app_id','left');
        $total = $db->count('id');
        return json(['code'=>0,'data'=>$info,'total'=>$total]);*/
    }

}
